package com.zcl.controller;

import com.zcl.pojo.Classes;
import com.zcl.pojo.PageResult;
import com.zcl.pojo.Student;
import com.zcl.service.ClassesService;
import com.zcl.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;
    @RequestMapping("/list")
    //model数据
    public String list(Model model){
        List<Student> students=studentService.findAll();
        //查询所有学生,将查询到的学生放入model,在list文件中
        model.addAttribute("student",students);
        //返回逻辑视图
        return "student/list";
    }
    //分页查询
    @RequestMapping("/findByPage")
    //@RequestParam对传入的参数进行说明,分页赋予默认值,非必须的,默认值为*
    public String findByPage(@RequestParam(required = false,defaultValue = "1") int page,
                             @RequestParam(required = false,defaultValue = "5") int pageSize, Model model){
        //开始分页
        PageResult<Student> pr=studentService.findByPage(page,pageSize);
        //将分页结果放入model数据,可在list.jsp用EL表达式调用
        model.addAttribute("pr",pr);
        //返回逻辑视图
        return "student/list";
    }

    //条件分页查询
    @RequestMapping("/search")
    //@RequestParam对传入的参数进行说明,分页赋予默认值,非必须的,默认值为*
    public String search(@RequestParam(required = false,defaultValue = "1") int page,
                         @RequestParam(required = false,defaultValue = "5") int pageSize,
                         Student student,
                         Model model){
        //开始分页(第几条数据,每页大小,封装的查询条件)
        PageResult<Student> pr=studentService.search(page,pageSize,student);
        //查询班级列表
        List<Classes> classes = classesService.findAll();
        //将分页结果放入model数据,可在list.jsp用EL表达式调用
        model.addAttribute("pr",pr);
        model.addAttribute("classes",classes);
        model.addAttribute("student",student);
        //返回逻辑视图
        return "student/list";
    }
    //跳转到学生页面
    @GetMapping("/edit")
    public String edit(Model model ,String sid){
        //查询所有班级
        List<Classes> classes =classesService.findAll();
        //放入到model,返回到edit页面
        model.addAttribute("classes",classes);
        //通过sid查询到学生对象
        Student student = studentService.findById(sid);
        //放入model
        model.addAttribute("student",student);
        return "student/edit";
    }
    @RequestMapping("/add")
    public String add(Student student){
        //添加学生
        studentService.insert(student);
        //返回视图,redirect重定向跳转
        return "redirect:/student/search.do";
    }
    //修改学生
    @RequestMapping("/update")
    public String update(Student student){
        //修改学生
        studentService.update(student);
        //返回视图,redirect重定向跳转
        return "redirect:/student/search.do";
    }
    //删除学生
    @RequestMapping("/delete")
    public String delete(Student student){
        //调用删除方法
        studentService.delete(student);
        //返回视图
        return "redirect:/student/search.do";
    }
}
